//
//  ShareCenterEngine.m
//  ShareCenter
//
//  Created by yutao on 14-2-19.
//  Copyright (c) 2014年 yutao. All rights reserved.
//

#import "ShareCenterEngine.h"
#import "ShareCenterService.h"

#import "SinaWBShareService.h"
#import "ZoneShareService.h"
#import "WXSessionShareService.h"

@interface ShareCenterEngine ()

@property (nonatomic, retain) NSMutableArray        *services;
@property (nonatomic, retain) NSMutableDictionary   *typeToService;

- (NSString *)keyForServiceType:(SNShareClientType)serviceType;

@end

@implementation ShareCenterEngine

static ShareCenterEngine *instance = nil;

+ (id)defaultEngine
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[ShareCenterEngine alloc] init];
    });
    return instance;
}

- (id)init
{
    self = [super init];
    if (self) {
        _services = [[NSMutableArray alloc] initWithCapacity:1];
        _typeToService = [[NSMutableDictionary alloc] initWithCapacity:1];
    }
    return self;
}

- (void)registerApplicationWithServicesType:(int)statementCount,...
{
    va_list arg_ptr;
    va_start(arg_ptr, statementCount);
    for (int i = 0; i < statementCount; i++)
    {
        SNShareClientType serviceType = va_arg(arg_ptr, SNShareClientType);
        [self registerService:[self serviceForServiceType:serviceType]];
    }
    va_end(arg_ptr);
    [self registerApplication];
}

- (void)registerApplication
{
    [self.services enumerateObjectsUsingBlock:^(ShareCenterService *obj, NSUInteger idx, BOOL *stop) {
        if ([obj respondsToSelector:@selector(registerApplication)]) {
            [obj registerApplication];
        }
    }];
}

- (ShareCenterService *)serviceForServiceType:(SNShareClientType)serviceType
{
    ShareCenterService * service = [self.typeToService objectForKey:[self keyForServiceType:serviceType]];
    
    if (service == nil)
    {
        switch (serviceType)
        {
            case SNShareClientTypeSinaWeibo:
            {
                service = [SinaWBShareService service];
            }
                break;
            case SNShareClientTypeWeixinTimeline:
            {
                service = [WXSessionShareService service];
            }
                break;
            case SNShareClientTypeQZone:
            {
                service = [ZoneShareService service];
            }
                break;
            default:
                break;
        }
    }
    
    return service;
}

- (void)registerService:(ShareCenterService *)service
{
    [self.services addObject:service];
    [self.typeToService setObject:service forKey:[self keyForServiceType:service.serviceInfo.serviceType]];
}

- (NSString *)keyForServiceType:(SNShareClientType)serviceType
{
    return [NSString stringWithFormat:@"SERVICE_%d", serviceType];
}
- (BOOL)isAvailableForServiceType:(SNShareClientType)serviceType
{
    ShareCenterService *service = [self serviceForServiceType:serviceType];
    return [service isAvailable];
}

- (NSArray *)availableServices
{
    NSMutableArray *availableServices = [[NSMutableArray alloc] initWithCapacity:1];
    [self.services enumerateObjectsUsingBlock:^(ShareCenterService *obj, NSUInteger idx, BOOL *stop) {
//        if ([self isAvailableForServiceType:obj.serviceInfo.serviceType]) {
//            [availableServices addObject:obj];
//        }
    }];
    
    return [availableServices autorelease];
}

#pragma mark - Service Interface

- (void)loginForServiceType:(SNShareClientType)serviceType
{
    ShareCenterService *service = [self serviceForServiceType:serviceType];
    if ([service respondsToSelector:@selector(login)]) {
        [service login];
    }
}

- (BOOL)logoutForServiceType:(SNShareClientType)serviceType
{
    ShareCenterService *service = [self serviceForServiceType:serviceType];
    if ([service respondsToSelector:@selector(logout)]) {
        return [service logout];
    }
    
    return NO;
}

- (BOOL)isValidForServiceType:(SNShareClientType)serviceType
{
    ShareCenterService *service = [self serviceForServiceType:serviceType];
    if ([service respondsToSelector:@selector(isValid)]) {
        return [service isValid];
    }
    
    return NO;
}

- (BOOL)handleOpenURL:(NSURL *)url
{
    __block BOOL handled = NO;
    [self.services enumerateObjectsUsingBlock:^(ShareCenterService *obj, NSUInteger idx, BOOL *stop) {
        if ([obj respondsToSelector:@selector(handleOpenURL:)]) {
            handled = [obj handleOpenURL:url];
        }
        
        if (handled) {
            *stop = YES;
        }
    }];
    
    return handled;
}

- (void)shareMessage:(ShareMessageInfo *)message forServiceType:(SNShareClientType)serviceType
{
    ShareCenterService *service = [self serviceForServiceType:serviceType];
    [service shareMessage:message];
}

- (void)loadUserInfoForServiceType:(SNShareClientType)serviceType
{
    ShareCenterService *service = [self serviceForServiceType:serviceType];
    if ([service respondsToSelector:@selector(loadUserInfo)]) {
        [service loadUserInfo];
    }
}

@end
